﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace CoderJoe.WoW.Services
{
	public class ItemService
	{
		private List<Item> ItemCache { get; set; }
        private List<ItemDetail> ItemDetailCache { get; set; }

		public ItemService()
		{
			ItemCache = new List<Item>();
            ItemDetailCache = new List<ItemDetail>();
		}

		public Image GetItemImage(string imageName)
		{
			CoderJoe.WoW.Proxies.Armory.WoWArmoryProxy proxy = new CoderJoe.WoW.Proxies.Armory.WoWArmoryProxy();
			return proxy.GetItemImage(imageName);
		}

		public Item FindItemByItemId(int itemId)
		{
			return
				(from i in ItemCache
				 where i.ItemId == itemId
				 select i).FirstOrDefault();
		}

		public Item FindOrLoadItem(int itemId)
		{
			Item result = FindItemByItemId(itemId);

			if (result == null)
				result = RefreshItem(itemId);

			return result;
		}

		public Item RefreshItem(int itemId)
		{
			Item existing = FindItemByItemId(itemId);
			if (existing != null)
			{
				ItemCache.Remove(existing);
			}

			CoderJoe.WoW.Proxies.Armory.WoWArmoryProxy proxy = new CoderJoe.WoW.Proxies.Armory.WoWArmoryProxy();
			Item remoteItem = proxy.GetItem(itemId);
			ItemCache.Add(remoteItem);
			return remoteItem;
		}

        public ItemDetail FindItemDetailByItemDetailId(int itemId)
        {
            return
                (from i in ItemDetailCache
                 where i.ItemId == itemId
                 select i).FirstOrDefault();
        }

        public ItemDetail FindOrLoadItemDetail(int itemId)
        {
            ItemDetail result = FindItemDetailByItemDetailId(itemId);

            if (result == null)
                result = RefreshItemDetail(itemId);

            return result;
        }

        public ItemDetail RefreshItemDetail(int itemId)
        {
            ItemDetail existing = FindItemDetailByItemDetailId(itemId);
            if (existing != null)
            {
                ItemDetailCache.Remove(existing);
            }

            CoderJoe.WoW.Proxies.Armory.WoWArmoryProxy proxy = new CoderJoe.WoW.Proxies.Armory.WoWArmoryProxy();
            ItemDetail remoteItemDetail = proxy.GetItemDetail(itemId);
            ItemDetailCache.Add(remoteItemDetail);
            return remoteItemDetail;
        }
	}
}
